import {toast} from "./commonUtils";

// TODO 小程序支付
export const miniProgram = (par: any) => {
    return new Promise((resolve, reject) => {
        uni.requestPayment({
            provider: 'wxpay', orderInfo: undefined,
            timeStamp: par.timeStamp,
            nonceStr: par.nonceStr,
            package: par.packageValue,
            signType: par.signType,
            paySign: par.paySign,
            success: (res: any) => {
                toast('支付成功');
                resolve(res);
            },
            fail: (err: any) => {
                if (err.errMsg === 'requestPayment:fail cancel') {
                    toast('支付已取消');
                } else {
                    toast('支付失败：' + (err.errMsg || '未知错误'));
                }
                reject(err);
            }
        });
    })
};

// TODO app支付
const payApp = (orderInfos: any) => {
    return new Promise((resolve, reject) => {
        uni.requestPayment({
            provider: 'wxpay',
            orderInfo: orderInfos,
            success(res: any) {
                toast('充值成功');
                resolve(res)
            },
            fail(err: any) {
                toast('支付失败');
                reject(err)
            },
        });
    })
};